package com.jxk.jms.service;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * @ClassName : ConsumerService  //类名
 * @Description :   //描述
 * @Author : jxk  //作者
 * @Date: 2020-09-21 16:28  //时间
 */
@Service
public class ConsumerService {
    @RabbitListener(queues = "demoQueue")
    public void recieved(String msg) {
        System.out.println("[demoQueue] recieved message: " + msg);
    }
    @RabbitListener(queues = "fanout.a")
    public void recieved2(String msg) {
        System.out.println("[fanout.a] recieved message: " + msg);
    }

    /**
     * basicAck：这个是手动确认消息已经成功消费，该方法有两个参数：第一个参数表示消息的 id；第二个参数 multiple 如果为 false，
     * 表示仅确认当前消息消费成功，如果为 true，则表示当前消息之前所有未被当前消费者确认的消息都消费成功。
     * basicNack：这个是告诉 RabbitMQ 当前消息未被成功消费，该方法有三个参数：第一个参数表示消息的 id；第二个参数 multiple 如果为 false，
     * 表示仅拒绝当前消息的消费，如果为 true，则表示拒绝当前消息之前所有未被当前消费者确认的消息；第三个参数 requeue 含义和前面所说的一样，被拒绝的消息是否重新入队。
     */
//    手动确认
//    @RabbitListener(queues = "fanout.a")
//    public void recieved2(Message message, Channel channel) {
//        long deliveryTag = message.getMessageProperties().getDeliveryTag();
//        try {
//            //消息消费的代码写到这里
//            String s = new String(message.getBody());
//            System.out.println("s = " + s);
//            //消费完成后，手动 ack
//            channel.basicAck(deliveryTag, false);
//        } catch (Exception e) {
//            //手动 nack
//            try {
//                channel.basicNack(deliveryTag, false, true);
//            } catch (IOException ex) {
//                ex.printStackTrace();
//            }
//        }
//    }
    @RabbitListener(queues = "fanout.b")
    public void recieved3(String msg) {
        System.out.println("[fanout.b] recieved message: " + msg);
    }
    @RabbitListener(queues = "fanout.c")
    public void recieved4(String msg) {
        System.out.println("[fanout.c] recieved message: " + msg);
    }
    // 类似的 需要相同的 routingKey
    @RabbitListener(queues = "direct.a")
    public void recieved8(String msg) {
        System.out.println("[direct.a] recieved message: " + msg);
    }

    @RabbitListener(queues = "topic.a")
    public void recieved5(String msg) {
        System.out.println("[topic.a] recieved message:" + msg);
    }
    @RabbitListener(queues = "topic.b")
    public void recieved6(String msg) {
        System.out.println("[topic.b] recieved message:" + msg);
    }
    @RabbitListener(queues = "topic.c")
    public void recieved7(String msg) {
        System.out.println("[topic.c] recieved message:" + msg);
    }
    @RabbitListener(queues = "name-queue")
    public void handler1(byte[] msg) {
        System.out.println("HeaderReceiver:name:"
                + new String(msg, 0, msg.length));
    }
    @RabbitListener(queues = "age-queue")
    public void handler2(byte[] msg) {
        System.out.println("HeaderReceiver:age:"
                + new String(msg, 0, msg.length));
    }
}
